﻿function CollectCompute() {
    //汇总合计
    $(".goodsCodeList").each(function () {
        var exits = 0;

        var gdcode = $(this).html();
        var gdqty = $(this).parent().find("td").eq(12).html();        //数量
        var gdnwt = $(this).parent().find("td").eq(11).html();       //净重
        //var gdcount = $(this).next("td:eq(2)").html();      //计数
        $(".csstotallist").each(function () {
            if ($(this).html() == gdcode) {
                var gdcount1 = $(this).parent().find("td").eq(4).html();
                var gdqty1 = $(this).parent().find("td").eq(2).html();
                var gdnwt1 = $(this).parent().find("td").eq(3).html();
                $(this).parent().find("td").eq(2).html(parseInt(gdqty1) + parseInt(gdqty));
                $(this).parent().find("td").eq(3).html(parseFloat(gdnwt1) + parseFloat(gdnwt));
                $(this).parent().find("td").eq(4).html(parseInt(gdcount1) + 1);
                exits = 1;
            }
        });
        if (exits == 0) {
            var strhtml = "<tr>";
            strhtml += "<td>" + $('#tabType tr').length + "</td>";
            strhtml += "<td class='csstotallist'>" + gdcode + "</td>";
            strhtml += "<td>" + gdqty + "</td>";
            strhtml += "<td>" + gdnwt + "</td>";
            strhtml += "<td>1</td>";
            strhtml += "</tr>";
            $("#tabType").append(strhtml);
        }

    });

}


///删除后的隐藏
function hidRow(obj) {
    var flag = 0;
    var gdcode = $(obj).parent().parent().find("td").eq(6).html();      //物品编码
    var gdqty = parseInt($(obj).parent().parent().find("td").eq(12).html());        //数量
    var gdnwt = parseFloat($(obj).parent().parent().find("td").eq(11).html());       //净重
    $(".csstotallist").each(function () {
        if ($(this).html() == gdcode) {
            var gdcount1 = parseInt($(this).parent().find("td").eq(4).html());
            var gdqty1 = parseInt($(this).parent().find("td").eq(2).html());
            var gdnwt1 = parseFloat($(this).parent().find("td").eq(3).html());
            if (gdcount1 == 1 && flag == 0) {
                $(this).parent().remove();
                flag = 1;
            }
            else if (flag == 0) {
                $(this).parent().find("td").eq(2).html(gdqty1 - gdqty);
                $(this).parent().find("td").eq(3).html(gdnwt1 - gdnwt);
                $(this).parent().find("td").eq(4).html(gdcount1 - 1);
                flag = 1;
            }
            return;
        }
    });
    $(obj).parent().parent().remove();
    if ($('#tab tr').length == 1) {
        $(".blong").val('');
        $(".dropGropphic_old").val('');
        $(".oldload").val('');
    }
}


//物品明细扫描回车事件操作
function outstoragebybarcode() {
    $.ajax({
        type: "post",
        url: "/ashx/product.ashx",
        data: {
            barcode: $("#txtBarcode").val()
                , otype: 'bar'
                , barbelong_old: $(".blong").val()
                , bargraphic_old: $(".graphic").val()
                , outid: $(".hidoutid").val()
                , oldload: $(".oldload").val()
                , operman: $(".hidOper").val()
        },
        success: function (msg) {
            var obj = eval('(' + msg + ')');
            if (obj[2].isau != "OK") {
                alert(obj[2].isau);
                $("#txtBarcode").val("");
                $("#txtBarcode").focus();
                return;
            }
            else {
                if ($(".blong").val() == "" && $(".blong").val() == "") {
                    $(".blong").val(obj[0].barbelong);
                    $(".graphic").val(obj[0].bargraphic);
                }
                else {
                    if ($(".blong").val() != obj[0].barbelong) {
                        alert('归属地不同');
                        return;
                    }
                    else if ($(".graphic").val() != obj[0].bargraphic) {
                        //                        alert($(".dropGropphic_old").val());
                        alert("graphic");
                        return;
                    }
                }
                $(".oldload").val(obj[0].loadnumv);

                var tr = "<tr>";
                tr += "<td>" + $('#tab tr').length + "</td>";
                tr += "<td style='display:none;' class='barcodeid'>" + obj[0].id + "</td>";
                tr += "<td>" + obj[0].barbelong + "</td>";
                tr += "<td>" + obj[0].bargraphic + "</td>";
                tr += "<td class='barvalue'>" + obj[0].barcodevalue + "</td>";
                tr += "<td>" + obj[0].stroageNo + "</td>";
                tr += "<td>" + obj[0].goodscode + "</td>";
                tr += "<td>" + obj[0].goodsname + "</td>";
                tr += "<td>" + obj[0].loadnumv + "</td>";
                tr += "<td>" + obj[1].customercode + "</td>";
                tr += "<td>" + obj[1].companyname + "</td>";
                tr += "<td>" + obj[0].nwt + "</td>";
                tr += "<td>" + obj[0].qty + "</td>";
                tr += "<td><img src='/images/jian.png' width='16' onclick='javascript:hidRow(this)' height='17' class='jian'/></td>";
                tr += "</tr>";
                $(".belong").val();
                $(".graphic").val();
                if (obj != null) {
                    $("#tab").append(tr);
                    $("#txtBarcode").val("");
                    $("#txtBarcode").focus();

                    var exits = 0;

                    $(".csstotallist").each(function () {
                        if ($(this).html() == obj[0].goodscode) {
                            var gdcount1 = $(this).parent().find("td").eq(4).html();
                            var gdqty1 = $(this).parent().find("td").eq(2).html();
                            var gdnwt1 = $(this).parent().find("td").eq(3).html();
                            $(this).parent().find("td").eq(2).html($('#tabType tr').length);
                            $(this).parent().find("td").eq(2).html(parseInt(obj[0].qty) + parseInt(gdqty1));
                            $(this).parent().find("td").eq(3).html(parseFloat(obj[0].nwt) + parseFloat(gdnwt1));
                            $(this).parent().find("td").eq(4).html(parseInt(gdcount1) + 1);
                            exits = 1;
                        }
                    });
                    if (exits == 0) {
                        var strhtml = "<tr>";
                        strhtml += "<td>" + $('#tabType tr').length + "</td>";
                        strhtml += "<td class='csstotallist'>" + obj[0].goodscode + "</td>";
                        strhtml += "<td>" + obj[0].qty + "</td>";
                        strhtml += "<td>" + obj[0].nwt + "</td>";
                        strhtml += "<td>1</td>";
                        strhtml += "</tr>";
                        $("#tabType").append(strhtml);
                    }
                }
            }
        }
    });
}

$(document).ready(function () {


    $("#txtBarcode").bind('keyup', function (event) {
        //条形码为空验证
        if ($("#txtBarcode").val() == "") {
            alert("条形码不能为空");
            return;
        }

        //回车
        if (event.keyCode == "13") {

            var strbarcode = "";
            $(".barcodevalue").each(function () {
                strbarcode += $(this).html();
                strbarcode += ",";
            });
            $(".barvalue").each(function () {
                if (strbarcode == "")
                    strbarcode += ",";
                strbarcode += $(this).html();
                strbarcode += ",";
            });

                  if (strbarcode.indexOf($("#txtBarcode").val()) >= 0) {
                alert('不能重复扫描');
                $("#txtBarcode").val("");
                return;
            }

            outstoragebybarcode();
        }
    });

    //提交事件
    $("#btnSave").click(function () {
        $("#btnSave").attr("disabled", "disabled");
        var str = "";

        var arrbarid = "";
        var arrbarvalue = "";
        $(".barcodeid").each(function () {
            arrbarid += $(this).html();
            arrbarid += ",";
        });
        $(".barvalue").each(function () {
            arrbarvalue += $(this).html();
            arrbarvalue += ",";
        });

        str += arrbarvalue;

        $.ajax({
            type: "post",
            url: "/ashx/product.ashx?outBar=" + str,
            data: {
                outno: $(".txtOutStorageCode").val()
                    , oper: $(".hidOper").val()
                    , ConsumMan: $(".txtConsumMan").val()
                    , barbelong_old: $(".blong").val()
                    , bargraphic_old: $(".graphic").val()
                    , outid: $(".hidoutid").val()
                    , outBar: arrbarid
                    , month: $(".dropYear").val() + $(".dropMonth").val()
                    , otype: "submit"
                    , oldload: ""
                    , remark: $(".txrRemark").val()
            },
            success: function (msg) {
                if (msg != null) {
                    var outid = $(".hidoutid").val();
                    outid = msg;
                    window.open("/RDLC/OutStorageRDLC.aspx?outStorID=" + outid, "_self");
                }
            }
        });

    });

    CollectCompute();
});

function delBarCodeData(obj) {
    if (confirm("确认删除吗？")) {
        var barcodevalue = $(obj).parent().parent().find("td").eq(4).html();      //条码

        $.ajax({
            type: "post",
            url: "/ashx/product.ashx",
            data: {
                otype: "delBar"
          , oper: $(".hidOper").val()
          , barvalue: barcodevalue
          , outid: $(".hidoutid").val()
            },
            success: function (msg) {
                if (msg != -1) {
                    hidRow(obj);
                }
                else {
                    alert('请先维护入库单，入库单重量超出');
                }
            }
        });
    }
}